package pack0902;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class DemoC1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try {
            //建立服务器
            ServerSocket ss = new ServerSocket(9989);
            System.out.println("服务器建立了");
            //建立服务端接字
            System.out.println("等待客户端连接");
            Socket socket = ss.accept();
            System.out.println("客户端已连接");

            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();

            byte[] bytes = new byte[100];
            while (true){
                String str = in.next();
                if ("exit".equals(str)){
                    break;
                }
                os.write(str.getBytes());

                int len = is.read(bytes);
                String str1 = new String(bytes,0,len);
                System.out.println(str1);
            }
            os.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
